<?php 
class ControllerCompareCompare extends Controller
{

    public function index2()
    {
        $this->language->load('compare/compare');

        $this->document->title = $this->language->get('heading_title');

        $this->document->breadcrumbs = array();

        $this->document->breadcrumbs[] = array(
            'href' => HTTP_SERVER . 'index.php?route=common/home',
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );

        $url = '';

        if (isset($this->request->get['keyword'])) {
            $url .= '&keyword=' . $this->request->get['keyword'];
        }

        if (isset($this->request->get['category_id'])) {
            $url .= '&category_id=' . $this->request->get['category_id'];
        }

        if (isset($this->request->get['description'])) {
            $url .= '&description=' . $this->request->get['description'];
        }

        if (isset($this->request->get['model'])) {
            $url .= '&model=' . $this->request->get['model'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->document->breadcrumbs[] = array(
            'href' => HTTP_SERVER . 'index.php?route=product/search' . $url,
            'text' => $this->language->get('heading_title'),
            'separator' => $this->language->get('text_separator')
        );

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_critea'] = $this->language->get('text_critea');
        $this->data['text_search'] = $this->language->get('text_search');
        $this->data['text_keyword'] = $this->language->get('text_keyword');
        $this->data['text_category'] = $this->language->get('text_category');
        $this->data['text_empty'] = $this->language->get('text_empty');
        $this->data['text_sort'] = $this->language->get('text_sort');

        $this->data['entry_search'] = $this->language->get('entry_search');
        $this->data['entry_description'] = $this->language->get('entry_description');
        $this->data['entry_model'] = $this->language->get('entry_model');

        $this->data['button_search'] = $this->language->get('button_search');

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'p.sort_order';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['keyword'])) {
            $this->data['keyword'] = $this->request->get['keyword'];
        } else {
            $this->data['keyword'] = '';
        }

        if (isset($this->request->get['category_id'])) {
            $this->data['category_id'] = $this->request->get['category_id'];
        } else {
            $this->data['category_id'] = '';
        }


        if (isset($this->request->get['description'])) {
            $this->data['description'] = $this->request->get['description'];
        } else {
            $this->data['description'] = '';
        }

        if (isset($this->request->get['model'])) {
            $this->data['model'] = $this->request->get['model'];
        } else {
            $this->data['model'] = '';
        }

        if (isset($this->request->get['keyword'])) {
            $this->load->model('catalog/product');

            $product_total = $this->model_catalog_product->getTotalProductsByKeyword($this->request->get['keyword'], isset($this->request->get['category_id']) ? $this->request->get['category_id'] : '', isset($this->request->get['description']) ? $this->request->get['description'] : '', isset($this->request->get['model']) ? $this->request->get['model'] : '');

            $product_tag_total = $this->model_catalog_product->getTotalProductsByTag($this->request->get['keyword'], isset($this->request->get['category_id']) ? $this->request->get['category_id'] : '');

            $product_total = max($product_total, $product_tag_total);

            if ($product_total) {
                $url = '';

                if (isset($this->request->get['category_id'])) {
                    $url .= '&category_id=' . $this->request->get['category_id'];
                }

                if (isset($this->request->get['description'])) {
                    $url .= '&description=' . $this->request->get['description'];
                }

                if (isset($this->request->get['model'])) {
                    $url .= '&model=' . $this->request->get['model'];
                }

                $this->load->model('catalog/review');
                $this->load->model('tool/seo_url');
                $this->load->model('tool/image');

                $this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');

                $this->data['products'] = array();

                $results = $this->model_catalog_product->getProductsByKeyword($this->request->get['keyword'], isset($this->request->get['category_id']) ? $this->request->get['category_id'] : '', isset($this->request->get['description']) ? $this->request->get['description'] : '', isset($this->request->get['model']) ? $this->request->get['model'] : '', $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));

                $tag_results = $this->model_catalog_product->getProductsByTag($this->request->get['keyword'], isset($this->request->get['category_id']) ? $this->request->get['category_id'] : '', $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));

                foreach ($results as $key => $value) {
                    $tag_results[$value['product_id']] = $results[$key];
                }

                //$product_total = count($tag_results);

                foreach ($tag_results as $result) {
                    if ($result['image']) {
                        $image = $result['image'];
                    } else {
                        $image = 'no_image.jpg';
                    }

                    if ($this->config->get('config_review')) {
                        $rating = $this->model_catalog_review->getAverageRating($result['product_id']);
                    } else {
                        $rating = false;
                    }

                    $special = FALSE;

                    $discount = $this->model_catalog_product->getProductDiscount($result['product_id']);

                    if ($discount) {
                        $price = $this->currency->format($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
                    } else {
                        $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));

                        $special = $this->model_catalog_product->getProductSpecial($result['product_id']);

                        if ($special) {
                            $special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
                        }
                    }

                    $options = $this->model_catalog_product->getProductOptions($result['product_id']);

                    if ($options) {
                        $add = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']);
                    } else {
                        $add = HTTPS_SERVER . 'index.php?route=checkout/cart&product_id=' . $result['product_id'];
                    }

                    $this->data['products'][] = array(
                        'name' => $result['name'],
                        'model' => $result['model'],
                        'rating' => $rating,
                        'stars' => sprintf($this->language->get('text_stars'), $rating),
                        'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
                        'price' => $price,
                        'options' => $options,
                        'special' => $special,
                        'href' => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&keyword=' . $this->request->get['keyword'] . $url . '&product_id=' . $result['product_id']),
                        'add' => $add
                    );
                }

                if (!$this->config->get('config_customer_price')) {
                    $this->data['display_price'] = TRUE;
                } elseif ($this->customer->isLogged()) {
                    $this->data['display_price'] = TRUE;
                } else {
                    $this->data['display_price'] = FALSE;
                }

                $url = '';

                if (isset($this->request->get['keyword'])) {
                    $url .= '&keyword=' . $this->request->get['keyword'];
                }

                if (isset($this->request->get['category_id'])) {
                    $url .= '&category_id=' . $this->request->get['category_id'];
                }

                if (isset($this->request->get['description'])) {
                    $url .= '&description=' . $this->request->get['description'];
                }

                if (isset($this->request->get['model'])) {
                    $url .= '&model=' . $this->request->get['model'];
                }

                if (isset($this->request->get['page'])) {
                    $url .= '&page=' . $this->request->get['page'];
                }

                $this->data['sorts'] = array();

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_default'),
                    'value' => 'p.sort_order-ASC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=p.sort_order&order=ASC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_name_asc'),
                    'value' => 'pd.name-ASC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=pd.name&order=ASC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_name_desc'),
                    'value' => 'pd.name-DESC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=pd.name&order=DESC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_price_asc'),
                    'value' => 'p.price-ASC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=p.price&order=ASC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_price_desc'),
                    'value' => 'p.price-DESC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=p.price&order=DESC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_rating_desc'),
                    'value' => 'rating-DESC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=rating&order=DESC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_rating_asc'),
                    'value' => 'rating-ASC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=rating&order=ASC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_model_asc'),
                    'value' => 'p.model-ASC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=p.model&order=ASC'
                );

                $this->data['sorts'][] = array(
                    'text' => $this->language->get('text_model_desc'),
                    'value' => 'p.model-DESC',
                    'href' => HTTP_SERVER . 'index.php?route=product/search' . $url . '&sort=p.model&order=DESC'
                );

                $url = '';

                if (isset($this->request->get['keyword'])) {
                    $url .= '&keyword=' . $this->request->get['keyword'];
                }

                if (isset($this->request->get['category_id'])) {
                    $url .= '&category_id=' . $this->request->get['category_id'];
                }

                if (isset($this->request->get['description'])) {
                    $url .= '&description=' . $this->request->get['description'];
                }

                if (isset($this->request->get['model'])) {
                    $url .= '&model=' . $this->request->get['model'];
                }

                if (isset($this->request->get['sort'])) {
                    $url .= '&sort=' . $this->request->get['sort'];
                }

                if (isset($this->request->get['order'])) {
                    $url .= '&order=' . $this->request->get['order'];
                }

                $pagination = new Pagination();
                $pagination->total = $product_total;
                $pagination->page = $page;
                $pagination->limit = $this->config->get('config_catalog_limit');
                $pagination->text = $this->language->get('text_pagination');
                $pagination->url = HTTP_SERVER . 'index.php?route=product/search' . $url . '&page={page}';

                $this->data['pagination'] = $pagination->render();

                $this->data['sort'] = $sort;
                $this->data['order'] = $order;
            }
        }

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/compare.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/product/compare.tpl';
        } else {
            $this->template = 'default/template/product/compare.tpl';
        }

        $this->children = array(
            'common/column_right',
            'common/column_left',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }

    private $error = array();

    public function index()
    {
        $this->language->load('compare/compare');

        if ($this->request->server['REQUEST_METHOD'] == 'GET' && isset($this->request->get['product_id'])) {

            if (isset($this->request->get['option'])) {
                $option = $this->request->get['option'];
            } else {
                $option = array();
            }

            $quantity = 1;

            $this->compare->add($this->request->get['product_id'], $option);

            $this->redirect(HTTPS_SERVER . 'index.php?route=compare/compare');

        } elseif ($this->request->server['REQUEST_METHOD'] == 'POST') {
            if (isset($this->request->post['remove'])) {
                foreach (array_keys($this->request->post['remove']) as $key) {
                    $this->compare->remove($key);
                }
            }

            if (isset($this->request->post['redirect'])) {
                $this->session->data['redirect'] = $this->request->post['redirect'];
            }

            if (isset($this->request->post['remove'])) {
                $this->redirect(HTTPS_SERVER . 'index.php?route=compare/compare');
            }
        }

        $this->document->title = $this->language->get('heading_title');

        $this->document->breadcrumbs = array();

        $this->document->breadcrumbs[] = array(
            'href' => HTTP_SERVER . 'index.php?route=common/home',
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );

        $this->document->breadcrumbs[] = array(
            'href' => HTTP_SERVER . 'index.php?route=compare/compare',
            'text' => $this->language->get('text_basket'),
            'separator' => $this->language->get('text_separator')
        );

        if ($this->compare->hasProducts()) {
            $this->data['heading_title'] = $this->language->get('heading_title');

            $this->data['text_select'] = $this->language->get('text_select');
            $this->data['text_sub_total'] = $this->language->get('text_sub_total');
            $this->data['text_discount'] = $this->language->get('text_discount');
            $this->data['text_weight'] = $this->language->get('text_weight');

            $this->data['column_remove'] = $this->language->get('column_remove');
            $this->data['column_image'] = $this->language->get('column_image');
            $this->data['column_name'] = $this->language->get('column_name');
            $this->data['column_model'] = $this->language->get('column_model');
            $this->data['column_quantity'] = $this->language->get('column_quantity');
            $this->data['column_price'] = $this->language->get('column_price');
            $this->data['column_total'] = $this->language->get('column_total');

            $this->data['button_update'] = $this->language->get('button_update');
            $this->data['button_shopping'] = $this->language->get('button_shopping');
            $this->data['button_checkout'] = $this->language->get('button_checkout');

            $this->data['action'] = HTTPS_SERVER . 'index.php?route=compare/compare';

            $this->load->model('tool/seo_url');
            $this->load->model('tool/image');

            $this->data['products'] = array();

            $this->load->model('catalog/product');
            foreach ($this->compare->getProducts() as $result) {
                $product_info = $this->model_catalog_product->getProduct($result['key']);

                if ($result['image']) {
                    $image = $result['image'];
                } else {
                    $image = 'no_image.jpg';
                }

                $discount = $this->model_catalog_product->getProductDiscount($result['key']);

                if ($discount) {
                    $price = $this->currency->format($this->tax->calculate($discount, $product_info['tax_class_id'], $this->config->get('config_tax')));
                    $special = FALSE;
                } else {
                    $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                    $special = $this->model_catalog_product->getProductSpecial($result['key']);
                    if ($special) {
                        $special = $this->currency->format($this->tax->calculate($special, $product_info['tax_class_id'], $this->config->get('config_tax')));
                    } else {
                        $special = FALSE;
                    }
                }

                if ($product_info['quantity'] <= 0) {
                    $stock = $product_info['stock'];
                } else {
                    if ($this->config->get('config_stock_display')) {
                        $stock = $product_info['quantity'];
                    } else {
                        $stock = $this->language->get('text_instock');
                    }
                }

                $this->load->model('catalog/review');
                if ($this->config->get('config_review')) {
                    $rating = $this->model_catalog_review->getAverageRating($result['key']);
                } else {
                    $rating = false;
                }

                $this->data['products'][] = array(
                    'key' => $result['key'],
                    'name' => $result['name'],
                    'model' => $result['model'],
                    'manufacturer' => $product_info['manufacturer'],
                    'description' => html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'),
                    'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')),
                    'price' => $price,
                    'stock' => $stock,
                    'rating' => $rating,
                    'href' => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id'])
                );
            }

            if (!$this->config->get('config_customer_price')) {
                $this->data['display_price'] = TRUE;
            } elseif ($this->customer->isLogged()) {
                $this->data['display_price'] = TRUE;
            } else {
                $this->data['display_price'] = FALSE;
            }

            $this->data['weight'] = FALSE;

            if (isset($this->session->data['redirect'])) {
                $this->data['continue'] = $this->session->data['redirect'];

                unset($this->session->data['redirect']);
            } else {
                $this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
            }

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/compare.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/product/compare.tpl';
            } else {
                $this->template = 'default/template/product/compare.tpl';
            }

            $this->children = array(
                'common/column_right',
                'common/column_left',
                'common/footer',
                'common/header'
            );

            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        } else {
            $this->data['heading_title'] = $this->language->get('heading_title');

            $this->data['text_error'] = $this->language->get('text_empty');

            $this->data['button_continue'] = $this->language->get('button_continue');

            $this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
            } else {
                $this->template = 'default/template/error/not_found.tpl';
            }

            $this->children = array(
                'common/column_right',
                'common/column_left',
                'common/footer',
                'common/header'
            );

            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        }
    }

    	public function callback() {
		$this->language->load('compare/compare');

		$this->load->model('tool/seo_url');

		if ($this->request->server['REQUEST_METHOD'] == 'POST') {

			if (isset($this->request->post['remove'])) {
	    		$result = explode('_', $this->request->post['remove']);
          		$this->compare->remove(trim($result[1]));
      		} else {
				if (isset($this->request->post['option'])) {
					$option = $this->request->post['option'];
				} else {
					$option = array();
				}

      			$this->compare->add($this->request->post['product_id'], $option);
			}
		}

		$output = '<table cellpadding="2" cellspacing="0" style="width: 100%;">';

		if ($this->compare->getProducts()) {

    		foreach ($this->compare->getProducts() as $product) {
      			$output .= '<tr>';
        		$output .= '<td width="1" valign="top" align="left"><span class="compare_remove" id="remove_ ' . $product['key'] . '" />&nbsp;</span></td><td width="1" valign="top" align="right">' .'&nbsp;</td>';
        		$output .= '<td align="left" valign="top"><a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $product['product_id']) . '">' . $product['name'] . '</a>';
          		$output .= '<div>';

				foreach ($product['option'] as $option) {
            		$output .= ' - <small style="color: #999;">' . $option['name'] . ' ' . $option['value'] . '</small><br />';
	            }

				$output .= '</div></td>';
				$output .= '</tr>';
      		}

			$output .= '</table>';
    		$output .= '<br />';

			$view = HTTP_SERVER . 'index.php?route=compare/compare';
      		$output .= '<div style="padding-top:5px;text-align:center;clear:both;"><a href="' . $view . '">' . $this->language->get('text_compare') . '</a></div>';
		} else {
			$output .= '<div style="text-align: center;">' . $this->language->get('text_empty') . '</div>';
		}

		$this->response->setOutput($output, $this->config->get('config_compression'));
	}

}

?>